home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 30
/
Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso
/
Aminet
/
dev
/
cross
/
GBDK-2.0.lha
/
GBDK
/
lib
/
mul.s
< prev
next >
Wrap
Text File
|
1998-10-01
|
1KB
|
45 lines
.include "global.s"
.area _CODE
;; 16-bit multiplication
;;
;; Entry conditions
;; BC = multiplicand
;; DE = multiplier
;;
;; Exit conditions
;; DE = less significant word of product
;;
;; Register used: AF,BC,DE,HL
.mul16::
.mulu16::
LD HL,#0x00 ; Product = 0
LD A,#15 ; Count = bit length - 1
;; Shift-and-add algorithm
;; If MSB of multiplier is 1, add multiplicand to partial product
;; Shift partial product, multiplier left 1 bit
.mlp:
SLA E ; Shift multiplier left 1 bit
RL D
JR NC,.mlp1 ; Jump if MSB of multiplier = 0
ADD HL,BC ; Add multiplicand to partial product
.mlp1:
ADD HL,HL ; Shift partial product left
DEC A
JR NZ,.mlp ; Continue until count = 0
;; Add multiplicand one last time if MSB of multiplier is 1
BIT 7,D ; Get MSB of multiplier
JR Z,.mend ; Exit if MSB of multiplier is 0
ADD HL,BC ; Add multiplicand to product
.mend:
LD D,H ; DE = result
LD E,L
RET
.mul8::
.mulu8::
LD B,#0x00 ; Sign extend is not necessary with mul
LD D,#0x00
JP .mul16